import { WXIconType } from "../../wxAPI_";
import * as NS from "../../Common/NetServices";
import * as NSB from "../../Common/NetServiceBase";
import { IsNull } from "../../TYLibrary/Common/GlobalExtend";
import { Wx_XSPPage, V_Product, V_Store, V_Goods } from "../../Common/Entitys";
import { Log } from "../../TYLibrary/System/Logger";
import { TimeSpan, EventArgs } from "../../TYLibrary/System/System";
import { Config } from "../../Config";
import { Wx_PageData, WXDataClassRuntimeDecorator } from "../../Common/EntityBase";

@WXDataClassRuntimeDecorator()
class IndexPageData extends Wx_PageData {
    public StoreIndex: Array<V_Store> = new Array<V_Store>();
    public GoodsList: Array<V_Goods> = new Array<V_Goods>();
    public SelectGOODS: Array<V_Goods> = new Array<V_Goods>();
    public IsShow: boolean = false;
    public IsDisable: boolean = true;
    public Count: number = 1;
    public authSetting: number = -1;
    public ParameterIndex: number = 0;
    public ColorIndex: number = 0;
    public product: boolean = null;
}

class IndexPage extends Wx_XSPPage {
    private m_GOODSNO: string;
    private m_SKUSID: number;
    public Data: IndexPageData = new IndexPageData();

    /**
     * 初始化页面信息
     * @param {*} options
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async InitData(options: any): Promise<void> {
        try {
            // 底部加载
            this.EditTabBar();
            wx.showToast({ title: "数据加载中......", icon: WXIconType.loading });
            //初始化
            this.App.ModuleManager.AuthManager.TYLoginUrl = Config.TYLogin;
            await this.App.ModuleManager.Init();
            this.Data.authSetting = this.App.WXLoginInfo.AuthSetting;
            this.Data.product = false;
            this.Data.ApplyChange(this);
            if (this.App.WXLoginInfo.OpenID == null) return;
            this.App.StoreIndex = await NS.QueryStoreIndex();
            let result = new Array<V_Store>();
            if (this.App.StoreIndex == null) {
                this.ShowError("数据没有找到！");
            } else {
                wx.setStorageSync("PLATFORMID", this.App.StoreIndex.PLATFORMID);
                wx.setStorageSync("MEMBERID", this.App.StoreIndex.MEMBERID);
                result.push(this.App.StoreIndex);
            }
            this.Data.StoreIndex = result;
            console.log("Data");
            console.log(this.Data);
            await this.Data.ApplyChange(this);
        } catch (e) {
            this.ShowError(e);
        } finally {
            wx.hideLoading();
        }
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async AddCart(e: any): Promise<void> {}

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async CloseHandler(e: any): Promise<void> {
        this.Data.IsShow = false;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async ShowproductHandler(e: any): Promise<void> {
        this.m_GOODSNO = e.currentTarget.dataset.goodsno;
        this.m_SKUSID = e.currentTarget.dataset.skusid;
        let c = new Array<V_Goods>();
        for (let s = 0; s < this.Data.StoreIndex[0].GOODS.length; s++) {
            if (this.Data.StoreIndex[0].GOODS[s].GOODSNO == this.m_GOODSNO)
                c.push(this.Data.StoreIndex[0].GOODS[s]);
        }
        this.Data.IsShow = true;
        this.Data.SelectGOODS = c;
        await this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async ColorHandler(e: any): Promise<void> {
        const index = e.currentTarget.dataset.index;
        this.Data.ColorIndex = index;
        await this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async ParameterHandler(e: any): Promise<void> {
        const index = e.currentTarget.dataset.index;
        this.Data.ParameterIndex = index;
        await this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async NumberHandler(e: any): Promise<void> {
        let type = e.currentTarget.dataset.type;
        if (type == 0) {
            if (this.Data.Count == 2) {
                this.Data.IsDisable = true;
                this.Data.Count = 1;
                this.Data.ApplyChange(this);
            } else {
                if (this.Data.Count > 1) this.Data.Count--;
                await this.Data.ApplyChange(this);
            }
        } else {
            this.Data.Count++;
            this.Data.IsDisable = false;
            this.Data.ApplyChange(this);
        }
    }

    /**
     * 确认
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async ConfirmCartHandler(e: any): Promise<void> {
        await NS.AddToCart(this.m_GOODSNO, this.Data.Count, this.m_SKUSID);
        wx.showToast({ title: "添加成功", icon: WXIconType.success });
        this.CanlerHandler(e);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async CanlerHandler(e: any): Promise<void> {
        this.Data.IsShow = false;
        await this.Data.ApplyChange(this);
    }
}

Page(new IndexPage());
